package com.lx.sync.consumer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lx.sync.core.model.SyncState;
import com.lx.sync.core.service.SyncStateService;
import com.lx.sync.consumer.entity.SyncStateEntity;
import com.lx.sync.consumer.mapper.SyncStateMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 数据库实现的同步状态服务
 */
@Service
public class DatabaseSyncStateServiceImpl extends ServiceImpl<SyncStateMapper, SyncStateEntity> implements SyncStateService {

    @Override
    public SyncState getLatestSyncState(String idxName) {
        SyncStateEntity entity = baseMapper.getLatestSyncState(idxName);
        if (entity == null) {
            return null;
        }

        SyncState state = new SyncState();
        BeanUtils.copyProperties(entity, state);
        return state;
    }

    @Override
    public boolean saveSyncState(SyncState syncState) {
        SyncStateEntity entity = new SyncStateEntity();
        BeanUtils.copyProperties(syncState, entity);
        return save(entity);
    }

    @Override
    public boolean updateSyncState(SyncState syncState) {
        SyncStateEntity entity = new SyncStateEntity();
        BeanUtils.copyProperties(syncState, entity);
        return updateById(entity);
    }
}
